package com.ilioili.appframe;
import java.io.File;
import java.util.ArrayList;










import util.bitmap.FileUtil;
import util.bitmap.Img;
import util.bitmap.ImgPool;
import util.bitmap.ImgView;
import util.bitmap.TestPictures;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewWithPictures extends BaseActivity {

	BaseAdapter adapter = new BaseAdapter() {
		@Override
		public View getView(int position, View v, ViewGroup parent) {
			ImgView iv = (ImgView) v;
			if(iv==null){
				iv = new ImgView(GridViewWithPictures.this);
				iv.setPadding(5, 5, 5, 5);
				iv.setRatioBaseOnWidth(1);
			}
			iv.setId(position);
			bitmapPool.bind(imgList.get(position), iv);
			return iv;
		}
		
		
		@Override
		public long getItemId(int position) {
			return 0;
		}
		
		@Override
		public Object getItem(int position) {
			return null;
		}
		
		@Override
		public int getCount() {
			return imgList.size();
		}
	};
	
	final ImgPool bitmapPool = new ImgPool(){
		@Override
		public void bitmapDecoded(Img<?> img, View v) {
			ImageView iv = (ImageView) v;
				iv.setImageBitmap(img.getBitmap());
				iv.startAnimation(getAnimation());
		}
		@Override
		public void bitmapNotDecoded(View v) {
			// TODO Auto-generated method stub
			ImageView iv = (ImageView) v;
			iv.setImageBitmap(null);
		}
		public Animation getAnimation(){
			Animation sa = new ScaleAnimation(0f, 1, 0f, 1, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
			sa.setDuration(1000);
			Animation aa = new AlphaAnimation(0, 1);
			aa.setDuration(1000);
			AnimationSet aSet = new AnimationSet(true);
			aSet.addAnimation(sa);
			aSet.addAnimation(aa);
			return aSet;
		}

		
	};
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		bitmapPool.shutdown();
	}
	
    ArrayList<Img<?>> imgList;
	@SuppressLint("HandlerLeak")
	@SuppressWarnings("rawtypes")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FileUtil.init(getApplication());
		final GridView gv = new GridView(this);
		final int column = 3;
		gv.setNumColumns(column);
		imgList = new ArrayList<Img<?>>();
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		gv.setLayoutParams(params);
		setContentView(gv);
		gv.setAdapter(adapter);
//		new Thread(new Runnable() {
//			@Override
//			public void run() {
				int width = getResources().getDisplayMetrics().widthPixels;
				long start = System.currentTimeMillis();
				File fileDir = FileUtil.getFileDir();
				File cacheDir = FileUtil.getCacheDir();
				for(int i=0; i<TestPictures.URLS.length; i++){
					Img img = new Img(cacheDir, fileDir, TestPictures.URLS[i]);
//									img.setSizeBaseOnWidth(width/3);
					img.setSize(width/column, width/column);
//									img.setSizeBaseOnWidth(300);
					imgList.add(img);
				}
				System.out.println(System.currentTimeMillis()-start);
//			}
//		}).start();
		
	}
}
